$(function () {
    var organId = $("#organId"),
        symptom = $("#symptom"),
        rank = $("#rank"),
        //allFields=[name,parentId,rank,picturePre],
        allFields = $([]).add(organId).add(symptom).add(rank),
        tips = $(".validateTips");

    function updateTips(t) {
        tips
            .text(t)
            .addClass("ui-state-highlight");
        setTimeout(function () {
            tips.removeClass("ui-state-highlight", 1500);
        }, 500);
    }

    function checkLength(o, n, min, max) {
        if (o.val().length > max || o.val().length < min) {
            o.addClass("ui-state-error");
            updateTips("" + n + " 的长度必须在 " +
                min + " 和 " + max + " 之间。");
            return false;
        } else {
            return true;
        }
    }

    function checkEmpity(o, n) {
        if (o.val() != null && o.val().length > 0) {
            return true
        } else {
            o.addClass("ui-state-error");
            updateTips("" + n + " 不能为空 ");
            return false;
        }
    }

    function checkRegexp(o, regexp, n) {
        if (!(regexp.test(o.val()))) {
            o.addClass("ui-state-error");
            updateTips(n);
            return false;
        } else {
            return true;
        }
    }

    $("#dialog-form").dialog({
        autoOpen: false,
        height: 400,
        width: 550,
        modal: true,
        buttons: {
            "添加": function () {
                var bValid = true;
                allFields.removeClass("ui-state-error");
                // 开始校验
                bValid = bValid && checkEmpity(organId, "器官");
                bValid = bValid && checkEmpity(symptom, "症状");
                bValid = bValid && checkEmpity(rank, "Rank");
                bValid = bValid && checkRegexp(rank, /^[1-9]\d*$/, "rank格式不正确。");

                // 如果校验成功
                if (bValid) {
                    API.editOrganSymptom({
                        "organId": organId.val(),
                        "symptom": symptom.val(),
                        "rank": rank.val()
                    }, function (res) {
                        if (res.code === '0') {
                            alert("成功");
                            window.location.href = "/body/symptom_list.htm?tabid=103_2";
                        } else {
                            alert(res.desc);
                        }
                    })
                }
            },
            Cancel: function () {
                $(this).dialog("close");
            }
        },
        close: function () {
            allFields.val("").removeClass("ui-state-error");
        }
    });

    $("#add-class").button().click(function () {
        $("#dialog-form").dialog("open");
    });
})

function deleteOrganSymptom(id) {
    API.deleteOrganSymptom({"id": id}, function (res) {
        if (res.code === "0") {
            alert("删除成功");
            window.location.reload();
        } else {
            alert(res.desc);
        }
    })
}

function doEditOrganSymptom(idV, organIdV, symptomV, rankV) {
    var id = $("#id"),
        organId = $("#organId"),
        symptom = $("#symptom"),
        rank = $("#rank"),
        allFields = $([]).add(name).add(organId).add(symptom).add(rank),
        tips = $(".validateTips");

    function updateTips(t) {
        tips
            .text(t)
            .addClass("ui-state-highlight");
        setTimeout(function () {
            tips.removeClass("ui-state-highlight", 1500);
        }, 500);
    }

    function checkLength(o, n, min, max) {
        if (o.val().length > max || o.val().length < min) {
            o.addClass("ui-state-error");
            updateTips("" + n + " 的长度必须在 " +
                min + " 和 " + max + " 之间。");
            return false;
        } else {
            return true;
        }
    }

    function checkEmpity(o, n) {
        if (o.val() != null && o.val().length > 0) {
            return true
        } else {
            o.addClass("ui-state-error");
            updateTips("" + n + " 不能为空 ");
            return false;
        }
    }

    function checkRegexp(o, regexp, n) {
        if (!(regexp.test(o.val()))) {
            o.addClass("ui-state-error");
            updateTips(n);
            return false;
        } else {
            return true;
        }
    }

    $("#dialog-form").dialog({
        autoOpen: false,
        height: 400,
        width: 550,
        modal: true,
        buttons: {
            "提交": function () {
                var bValid = true;
                allFields.removeClass("ui-state-error");
                // 开始校验
                bValid = bValid && checkEmpity(organId, "器官");
                bValid = bValid && checkEmpity(symptom, "症状");
                bValid = bValid && checkEmpity(rank, "Rank");
                bValid = bValid && checkRegexp(rank, /^[1-9]\d*$/, "rank格式不正确。");

                // 如果校验成功
                if (bValid) {
                    API.editOrganSymptom({
                        "id": id.val(),
                        "organId": organId.val(),
                        "symptom": symptom.val(),
                        "rank": rank.val()
                    }, function (res) {
                        if (res.code === '0') {
                            alert("成功");
                            window.location.href = "/body/symptom_list.htm?tabid=103_2";
                        } else {
                            alert(res.desc);
                        }
                    })
                }
            },
            Cancel: function () {
                $(this).dialog("close");
            }
        },
        close: function () {
            allFields.val("").removeClass("ui-state-error");
        }
    });
    id.val(idV);
    organId.val(organIdV);
    symptom.val(symptomV);
    rank.val(rankV);
    $("#dialog-form").dialog("open");
}

function exportDate(obj, code) {
    if (UTIL.checkButton(obj)) {

        API.managementMigrateExcute({"code": code}, function (res) {
            if (res.code === '0') {
                alert("执行成功！");
            } else {
                alert(res.desc);
            }
        });

        UTIL.releaseButton(obj);
    }
}